
<template>
<div class="myinfo">
  <div class="form-box">
    <u-form :model="form" ref="uForm" label-width="100px">
      <u-form-item label="姓名"><u-input v-model="form.nickname" /></u-form-item>
      <u-form-item label="手机号"><u-input v-model="form.phone" /></u-form-item>
      <u-form-item label="头像">
        <u-upload :action="action" max-count="1" :file-list="fileList" @on-success="successFile" @on-remove="removeFile"></u-upload>
      </u-form-item>
    </u-form>
  </div>
  <div class="primary-button" @click="submit" >保存</div>
</div>
</template>
<script>
import { mapGetters } from 'vuex'
export default {
  name: "myinfo",
  data() {
    return {
      form: {
        id: '',
        nickname: '',
        avatar_url: '',
        phone: ''
      },
      action: this.$baseUrl + '/api/upload/file',
      fileList: [],
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  onLoad() {
    if (!this.userInfo.id) {
      uni.showModal({
        title: '提示',
        content: '请先登录',
        showCancel: true,
        success: () => {
          uni.navigateTo({
            url: '/pages/user/login'
          })
        }
      })
    }
    uni.showLoading({
      title: '加载中'
    })
    this.$api.getUserInfo({
      id: this.userInfo.id
    }).then(res => {
      this.form = res
      if (res.avatar_url) {
        this.fileList = [{
          url: res.avatar_url
        }]
      }
    }).finally(() => {
      uni.hideLoading()
    })
  },
  methods: {
    successFile(e) {
      // 上传成功
      this.form.avatar_url = e.data.url
    },
    removeFile() {
      // 删除图片
      this.form.avatar_url = ''
    },
    submit() {

    }
  }
}
</script>

<style lang="scss">
.myinfo {
  height: 100%;
  background-color: #F5F7FA;
  padding: 20rpx;
  .primary-button {
    margin-top: 15px;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 41px;
    line-height: 41px;
    font-size: 16px;
    color: #fff;
    background-color: #FF770F;
    border-radius: 5px;
    box-sizing: border-box;
  }
  .form-box {
    border-radius: 10px;
    background-color: #FFFFFF;
    padding: 20rpx;
  }
}
</style>
